<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>ViTE博客管理后台</title>
    <meta name="description" content="ViTE博客管理后台">
    <meta name="keywords" content="ViTE博客管理后台">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">


    <!-- Theme included stylesheets -->
    <link href="../plugins/quill/quill.snow.css" rel="stylesheet">
    <link href="../plugins/quill/quill.bubble.css" rel="stylesheet">

    <!-- Core build with no theme, formatting, non-essential modules -->
    <link href="../plugins/quill/quill.core.css" rel="stylesheet">
    <!-- Main Quill library -->
    <script src="../plugins/quill/quill.js"></script>
    <!--        <script src="../plugins/quill/quill.core.js"></script>-->
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>网站管理<small>用户管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>网站管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="用户账号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findSearchPage()" class="dalfBut">查询</el-button>
<!--                <el-button type="primary" class="butT" @click="handleCreate()">新增用户</el-button>-->
            </div>

            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="用户账号" align="center" prop="user.account">
                </el-table-column>
                <el-table-column label="用户昵称" align="center" prop="user.nickname">
                </el-table-column>
                <el-table-column label="用户角色" align="center" prop="role.name">
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row.user)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.user)">删除</el-button>
                        <el-button size="mini" type="warning" @click="showSetRoleDialog(scope.row.user)">更改角色</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!--更改角色选择框-->
            <div>
                <el-dialog v-model="userId"
                           title="更改角色"
                           :visible.sync="setRoleDialogVisible"
                           width="27%"
                >
                    <div class="select-container">
                        <el-select v-model="selectV" filterable placeholder="请选择">
                            <el-option
                                    v-for="item in roleList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                            ></el-option>
                        </el-select>
                        <span slot="footer" class="dialog-footer">
                            <el-button @click="setRoleDialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="setRole(userId, selectV)">授 权</el-button>
                    </span>
                    </div>


                </el-dialog>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col>
                                <el-form-item label="用户账号" prop="user.account">
                                    <el-input v-model="formData.account"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="用户呢称" prop="user.nickname">
                                    <el-input v-model="formData.nickname"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="用户密码" prop="user.password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑" :visible.sync="dialogFormVisibleEdit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>

                            <el-col>
                                <el-form-item label="用户账号" prop="user.account">
                                    <el-input v-model="formData.account"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="用户昵称" prop="user.nickname">
                                    <el-input v-model="formData.nickname"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="用户密码" prop="user.password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/quill/vue-quill-editor.js"></script>
<script>

    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',
            editorOption: {
                theme: 'snow',
                boundary: document.body,
                modules: {
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'],
                        ['blockquote', 'code-block'],
                        [{'header': 1}, {'header': 2}],
                        [{'list': 'ordered'}, {'list': 'bullet'}],
                        [{'script': 'sub'}, {'script': 'super'}],
                        [{'indent': '-1'}, {'indent': '+1'}],
                        [{'direction': 'rtl'}],
                        [{'size': ['small', false, 'large', 'huge']}],
                        [{'header': [1, 2, 3, 4, 5, 6, false]}],
                        [{'color': []}, {'background': []}],
                        [{'font': []}],
                        [{'align': []}],
                        ['clean'],
                        ['link', 'image', 'video']
                    ]
                },
                placeholder: 'Insert text here ...',
                readOnly: false
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 5,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            subjectList: [],
            roleList: [], // 所有角色
            tableData: {},
            fbChoiceList: [{
                content: "",
            }],
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisibleEdit: false,
            setRoleDialogVisible: false,
            userId: '',
            selectV: '',
            rules: {//校验规则
                // name: [{required: true, message: '必填项', trigger: 'blur'}],
            }

        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            handleAvatarSuccess() {

            },
            beforeAvatarUpload() {

            },
            showSetRoleDialog(user) {

                axios.get("/admin/user/getAllRole").then((res) => {
                    if (res.data.success) {
                        this.roleList = res.data.data;
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
                this.userId = user.id;
                this.setRoleDialogVisible = true;

            },
            setRole(userId, roleId) {
                axios.get("/admin/user/setRoleForUser/" + userId, {
                    params: {
                        roleId: roleId
                    }
                }).then((res) => {
                    if (res.data.success) {
                        this.$message.success("修改成功");
                        this.findPage();
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    } else {
                        this.$message.error("系统异常")
                    }

                })
                this.setRoleDialogVisible = false
            },
            //编辑
            handleEdit() {
                axios.post("/admin/user/update", this.formData).then((res) => {
                    if (res.data.success) {
                        this.$message({
                            message: '恭喜你，编辑成功',
                            type: 'success'
                        });
                        this.dialogFormVisibleEdit = false;
                        this.findPage();
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
            },
            //添加
            handleAdd() {
                axios.post("/admin/user/add", this.formData).then((res) => {
                    if (res.data.success) {
                        this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                        this.dialogFormVisible = false;
                        this.findPage();
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
            },
            findSearchPage() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //分页查询
            findPage() {
                axios.post("/admin/user/userList", this.pagination).then((res) => {
                    if (res.data.success) {
                        this.dataList = res.data.data.list;
                        this.pagination.total = res.data.data.total;
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.subjects = [];
                this.tableData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(user) {
                this.resetForm();
                this.dialogFormVisibleEdit = true;
                this.formData = user;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            handleDelete(user) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/admin/user/delete/" + user.id).then((res) => {
                        if (res.data.success) {
                            this.$message.success("删除成功");
                            this.findPage();
                        }
                    }).catch((error) => {
                        if (error == "Error: Request failed with status code 403") {
                            this.$message.error("无操作权限");
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            }
        }
    });
    Vue.use(window.VueQuillEditor);
</script>
</html>
